#Conways game of life
#Think it should be a good short-term project :P
#list of lists is to be called matrix hereafter
import random

class grid():
    def __init__(self, x, y):
        #set running state
        self._running = True

        #get width + height
        width = x
        height = y

        #create the main (empty) matrix according to height and width
        coordinates = [ [0] * width for i in range(height)]

        #just for testing
        i = 0

        coordinates[3][4] = 1
        coordinates[3][5] = 1
        coordinates[3][6] = 1
        #populate with random 1's
        #for x in range (0, (int((width * height) / 10))):
        #    coordinates[random.randrange(0, (width - 1))][random.randrange(0, (height - 1))] = 1
        #initiate main loop
        while self._running:
            i += 1

            print '\nGeneration', i
            for x in coordinates:
                print ''.join(str(e) for e in  x)
            #create editing matrix
            object_buffer = list(coordinates)

            #run through each object
            for y in enumerate(object_buffer):
                for x in enumerate(y[1]):
                    counter = 0
                    current_pos = coordinates[x[0]][y[0]]
                    _x = x[0]
                    _y = y[0]
                    print _x, _y
                    #corners

                    """#top-left
                    if not x[0] and not y[0]:
                        counter += coordinates[width - 1][height - 1]
                    elif x[0] and not y[0]:
                        counter += coordinates[width - 1][y[0] - 1]
                    elif not x[0] and y[0]:
                        counter += coordinates[x[0] - 1][height - 1]
                    else:
                        counter += coordinates[x[0] - 1][y[0] - 1]

                    #top-right
                    if x[0] == width - 1 and not y[0]:
                        counter += coordinates[0][height - 1]
                    elif not x[0] == width - 1 and not y[0]:
                        counter += coordinates[x[0] + 1][height - 1]
                    elif x[0] == width - 1 and y[0]:
                        counter += coordinates[0][y[0] - 1]
                    else:
                        counter += coordinates[x[0] + 1][y[0] - 1]

                    #bottom-left
                    if not x[0] and y[0] == height - 1:
                        counter += coordinates[width - 1][0]
                    elif not x[0] and not y[0] == height - 1:
                        counter += coordinates[width - 1][y[0] + 1]
                    elif x[0] and y[0] == height - 1:
                        counter += coordinates[x[0] - 1][0]
                    else:
                        counter += coordinates[x[0] - 1][y[0] + 1]

                    #bottom-right
                    if x[0] == width - 1 and y[0] == height - 1:
                        counter += coordinates[0][0]
                    elif x[0] == width - 1 and not y[0] == height - 1:
                        counter += coordinates[0][y[0] - 1]
                    elif not x[0] == width - 1 and y[0] == height - 1:
                        counter += coordinates[x[0] + 1][0]
                    else:
                        counter += coordinates[x[0] + 1][y[0] + 1]

                    """
                    #sides

                    #left
                    if not x[0]:
                        counter_3 = coordinates[width - 1][y[0]]
                        print counter_3
                    else:
                        counter_3 = coordinates[(x[0] - 1)][y[0]]
                        print counter_3

                    #right
                    if x[0] == (width - 1):
                        counter_2 = coordinates[0][y[0]]
                        print counter_2
                    else:
                        counter_2 = coordinates[x[0] + 1][y[0]]
                        print counter_2

                    #top
                    if not y[0]:
                        counter_1 = coordinates[x[0]][height - 1]
                        print counter_1
                    else:
                        counter_1 = coordinates[x[0]][y[0] - 1]
                        print counter_1

                    #bottom
                    if y[0] == (height - 1):
                        counter_0 = coordinates[x[0]][0]
                        print counter_0
                    else:
                        counter_0 = coordinates[x[0]][y[0] + 1]
                        print counter_0

                    ##RULES##

                    if current_pos and counter < 2:#if the particle is currently alive
                        object_buffer[x[0]][y[0]] = 0
                        #print "(", x[0], ",", y[0], ") - 1"

                    if current_pos and counter > 3:#if there are more than 3 surrounding cells alive
                        object_buffer[x[0]][y[0]] = 0

                    if not current_pos and counter is 3:#or check for 3 squares around
                        object_buffer[x[0]][y[0]] = 1
                        #print "(", x[0], ",", y[0], ") - 0"
                    #print '(', y[0], ',', x[0], ') -', current_pos, '-', counter


            #overwrite main list - equivalent to screen.flip()
            coordinates = list(object_buffer)




            #to control the number of loops for now - testing
            if i == 1:
                self._running = False

if __name__ == "__main__":
    grid(10, 10)
    raw_input('\n\nSimulation Finished')
